package com.kehutong.house.service;

import com.kehutong.house.entity.Building;
import com.kehutong.house.entity.Community;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindOne;
import org.coraframework.util.Objects;


@WebService("/basic/building/service/api")
public class BuildServiceApi {
	private static final Logger logger = LoggerFactory.getLogger(BuildServiceApi.class);

	@Inject
	private JdbcSession jdbcSession;
	@Inject
	private CommunityServiceApi communityService;

	@ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
    	return jdbcSession.findArray(Building.class)
    			.eq(jsonObject, "id", "communityNo")
    			.like(jsonObject, "name")
				.eq("deleted", false)
				.order("sort", "desc")
				.exe();
    }

    @ReqMapping("/get")
    public Object get(Building item) {
    	return item;
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(Building building) {
		FindOne<Building> find = jdbcSession.findOne(Building.class)
				.eq("deleted", false)
				.eq("communityNo", building.getCommunityNo())
				.eq("name", building.getName());

		if(Objects.nonEmpty(building.getUnitName())){
			find.eq("unitName", building.getUnitName());
		}

		Community com = Pool.get(Community.class, building.getCommunityNo());
		building.setDepartmentNo(com.getParent_id());
		building.setDepartmentNos(com.getParent_ids());

		Building item = find.exe();

		if(Objects.nonNull(item)){
			return Result.error(0, "当前小区下已有相同的楼栋信息，请修改后重试");
		}
		jdbcSession.insert(building);
    	return Result.success(building);
    }

    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(Building building) throws Exception {
		FindOne<Building> find = jdbcSession.findOne(Building.class)
				.eq("deleted", false)
				.eq("communityNo", building.getCommunityNo())
				.eq("name", building.getName())
				.noEq("id", building.getId());

		if(Objects.nonEmpty(building.getUnitName())){
			find.eq("unitName", building.getUnitName());
		}

		Building item = find.exe();

		if(Objects.nonNull(item)){
			return Result.error(0, "当前小区下已有相同的楼栋信息，请修改后重试");
		}

    	jdbcSession.updateById(building);
    	return Result.success(building);
    }

    @ReqMapping("/delete")
	public Object delete(Building item) {
		item.setDeleted(true);
		jdbcSession.updateById(item);

		return Result.success();
	}
	@ReqMapping("/buildingList")
	public Object buildingList(JSONObject json) {
		logger.debug("{}=========", this.hashCode());
		Page<Building> page = jdbcSession.findPage(Building.class)
				.markBegin()
				.in(json, "id")
				.in(json, "communityNo")
				.markEnd()
				.eq("deleted", false)
				.between("createTime", json, "beginTime", "endTime")
				.like(json, "name", "unitName", "employeeNos")
				.page(json).exe();
		return page;
	}
}
